package com.jhgsys.internal.hazard.service.impl;

import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.hazard.dto.HazardExamineConfigDto;
import com.jhgsys.internal.hazard.entity.HazardExamineConfig;
import com.jhgsys.internal.hazard.entity.HazardExamineDtlConfig;
import com.jhgsys.internal.hazard.mapper.HazardExamineConfigMapper;
import com.jhgsys.internal.hazard.service.IHazardExamineConfigService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.service.IHazardExamineDtlConfigService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.*;
import java.util.stream.Collectors;

/**
 *  Service实现
 *
 * @author Lujiachen
 * @date 2024-08-13 08:53:55
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HazardExamineConfigServiceImpl extends ServiceImpl<HazardExamineConfigMapper, HazardExamineConfig> implements IHazardExamineConfigService {


    @Autowired
    IHazardExamineDtlConfigService hazardExamineDtlConfigService;
    /**
     * 查询（分页）
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     * @param request QueryRequest
     * @param hazardExamineConfig hazardExamineConfig
     * @return IPage<HazardExamineConfig>
    */
    @Override
    public IPage<HazardExamineConfig> findHazardExamineConfigPage(QueryRequest request, HazardExamineConfig hazardExamineConfig) {
        LambdaQueryWrapper<HazardExamineConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineConfig::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<HazardExamineConfig> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     * @param hazardExamineConfig hazardExamineConfig
     * @return List<HazardExamineConfig>
     */
    @Override
    public List<HazardExamineConfig> findHazardExamineConfigList(HazardExamineConfig hazardExamineConfig) {
	    LambdaQueryWrapper<HazardExamineConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineConfig::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author Lujiachen
    * @date 2024-08-13 08:53:55
    * @param id
    * @return HazardExamineConfig
    */
    @Override
    public HazardExamineConfig findHazardExamineConfigById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     * @param hazardExamineConfig hazardExamineConfig
     */
    @Override
    @Transactional
    public void createHazardExamineConfig(HazardExamineConfig hazardExamineConfig) {
        hazardExamineConfig.setCreatedTime(new Date());
        this.save(hazardExamineConfig);
    }

    /**
     * 修改
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     * @param hazardExamineConfig hazardExamineConfig
     */
    @Override
    @Transactional
    public void updateHazardExamineConfig(HazardExamineConfig hazardExamineConfig) {
        hazardExamineConfig.setUpdatedTime(new Date());
        this.saveOrUpdate(hazardExamineConfig);
    }

    /**
     * 逻辑删除
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     * @param hazardExamineConfig hazardExamineConfig
     * @param hazardExamineConfigIds hazardExamineConfigIds
     */
    @Override
    @Transactional
    public void deleteHazardExamineConfig(HazardExamineConfig hazardExamineConfig,String hazardExamineConfigIds) {
        List<String> list = Arrays.asList(hazardExamineConfigIds.split(StringPool.COMMA));
        this.baseMapper.update(hazardExamineConfig,new LambdaQueryWrapper<HazardExamineConfig>().in(HazardExamineConfig::getExamineConfigId,list));
	}

    /**
    * 物理删除
    * @author Lujiachen
    * @date 2024-08-13 08:53:55
    * @param hazardExamineConfigIds hazardExamineConfigIds
    */
    @Override
    @Transactional
    public void physicallyDeleteHazardExamineConfig(String hazardExamineConfigIds) {
        List<String> list = Arrays.asList(hazardExamineConfigIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardExamineConfig> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardExamineConfig::getExamineConfigId,list);
        this.remove(wapper);
    }

    @Override
    public List<HazardExamineConfig> findHazardExamineConfigListByTenantId(String currentTenantId) {
        LambdaQueryWrapper<HazardExamineConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineConfig::getDeletedFlg, 0);
        queryWrapper.eq(HazardExamineConfig::getTenantId, currentTenantId);
        // TODO 设置查询条件
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public void createHazardExamineConfigList(List<HazardExamineConfigDto> hazardExamineConfigDtoList) {
        for(HazardExamineConfigDto hazardExamineConfigDto:hazardExamineConfigDtoList ){
            LambdaQueryWrapper<HazardExamineConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(HazardExamineConfig::getDeletedFlg, 0);
            queryWrapper.eq(HazardExamineConfig::getTenantId, hazardExamineConfigDto.getTenantId());
            queryWrapper.eq(HazardExamineConfig::getExamineType,hazardExamineConfigDto.getExamineType());
            List<HazardExamineConfig> list = this.baseMapper.selectList(queryWrapper);
            HazardExamineConfig hazardExamineConfig = BeanHelper.copyProperties(hazardExamineConfigDto,HazardExamineConfig.class);
            if(!CollectionUtils.isNotEmpty(list)){
                hazardExamineConfig.setStatus("1");
                hazardExamineConfig.setExamineConfigId(UUID.randomUUID().toString().replace("-",""));
                createHazardExamineConfig(hazardExamineConfig);
            }else {
                hazardExamineConfig.setExamineConfigId(list.get(0).getExamineConfigId());
                updateHazardExamineConfig(hazardExamineConfig);
            }
            if(hazardExamineConfigDto.getDtlConfigList()!=null&& hazardExamineConfigDto.getDtlConfigList().size()>0){
                saveExamineDtlConfig(hazardExamineConfigDto.getDtlConfigList(),hazardExamineConfig);
            }else {
                HazardExamineDtlConfig hazardExamineDtlConfigLocal = new HazardExamineDtlConfig();
                hazardExamineDtlConfigLocal.setExamineConfigId(hazardExamineConfig.getExamineConfigId());
                //原先库里的子表
                List<HazardExamineDtlConfig> dtlConfigLocalList = hazardExamineDtlConfigService.findHazardExamineDtlConfigListByExamineConfigId(hazardExamineDtlConfigLocal);
                //跟传来的对比
                hazardExamineDtlConfigService.removeBatchByIds(dtlConfigLocalList);
            }
        }

    }

    @Override
    public void updateHazardExamineConfigList(List<HazardExamineConfigDto> hazardExamineConfigDtoList) {
        for(HazardExamineConfigDto hazardExamineConfigDto:hazardExamineConfigDtoList ){
            HazardExamineConfig hazardExamineConfig = BeanHelper.copyProperties(hazardExamineConfigDto,HazardExamineConfig.class);
            hazardExamineConfig.setStatus("1");
            updateHazardExamineConfig(hazardExamineConfig);
            if(hazardExamineConfigDto.getDtlConfigList()!=null&& hazardExamineConfigDto.getDtlConfigList().size()>0){
                saveExamineDtlConfig(hazardExamineConfigDto.getDtlConfigList(),hazardExamineConfig);
            }else {
                HazardExamineDtlConfig hazardExamineDtlConfigLocal = new HazardExamineDtlConfig();
                hazardExamineDtlConfigLocal.setExamineConfigId(hazardExamineConfig.getExamineConfigId());
                //原先库里的子表
                List<HazardExamineDtlConfig> dtlConfigLocalList = hazardExamineDtlConfigService.findHazardExamineDtlConfigListByExamineConfigId(hazardExamineDtlConfigLocal);
                //跟传来的对比
                 hazardExamineDtlConfigService.removeBatchByIds(dtlConfigLocalList);
            }
        }
    }

    private void saveExamineDtlConfig(List<HazardExamineDtlConfig> hazardExamineDtlConfig,HazardExamineConfig hazardExamineConfig) {
            if (CollectionUtils.isNotEmpty(hazardExamineDtlConfig)) {
                List<HazardExamineDtlConfig> addList = new ArrayList<>();
                List<HazardExamineDtlConfig> updateList = new ArrayList<>();
                HazardExamineDtlConfig hazardExamineDtlConfigLocal = new HazardExamineDtlConfig();
                hazardExamineDtlConfigLocal.setExamineConfigId(hazardExamineConfig.getExamineConfigId());
                //原先库里的子表
                List<HazardExamineDtlConfig> dtlConfigLocalList = hazardExamineDtlConfigService.findHazardExamineDtlConfigListByExamineConfigId(hazardExamineDtlConfigLocal);
                //跟传来的对比
                for(HazardExamineDtlConfig hazardExamineDtlConfig1:dtlConfigLocalList){
                    if(!hazardExamineDtlConfig.contains(hazardExamineDtlConfig1)){
                        hazardExamineDtlConfigService.removeById(hazardExamineDtlConfig1.getExamineDtlConfigId());
                    }
                }
                for(HazardExamineDtlConfig dtlConfig:hazardExamineDtlConfig){
                    if(StringUtils.isNotBlank(dtlConfig.getExamineDtlConfigId())&&!dtlConfig.getExamineDtlConfigId().equals("undefined")){
                        dtlConfig.setUpdatedBy(hazardExamineConfig.getUpdatedBy());
                        dtlConfig.setUpdatedTime(new Date());
                        updateList.add(dtlConfig);
                    }else {
                        dtlConfig.setCreatedTime(new Date());
                        dtlConfig.setCreatedBy(hazardExamineConfig.getCreatedBy());
                        dtlConfig.setExamineConfigId(hazardExamineConfig.getExamineConfigId());
                        dtlConfig.setExamineDtlConfigId("");
                        addList.add(dtlConfig);
                    }
                }
                hazardExamineDtlConfigService.saveBatch(addList);
                hazardExamineDtlConfigService.saveOrUpdateBatch(updateList);
            }
    }
}
